#include "chooselevelscene.h"
#include <QIcon>
#include <QPixmap>
#include <QPainter>
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QDebug>
#include <QLabel>
#include <QPushButton>
#include <QTimer>
#include <QSound>
#include "mypushbutton.h"
#include "playscene.h"

ChooseLevelScene::ChooseLevelScene(QMainWindow *parent) : QMainWindow(parent)
{
    setWindowTitle("选择关卡");
    setWindowIcon(QIcon(":/title_icon.png"));
    this->setFixedSize(320, 583);
    //创建菜单栏
    QMenuBar* bar = new QMenuBar(this);
    setMenuBar(bar);
    //创建开始菜单
    QMenu* menu = bar->addMenu("开始");
    menu->setParent(bar);
    //创建菜单项
    QAction* actionQuit = menu->addAction("退出");
    actionQuit->setParent(menu);
    connect(actionQuit, &QAction::triggered, [=](){
        this->close();
    });

    //创建返回按键
    m_backBtn = new MyPushButton(":/BackButton.png", ":/BackButtonSelected.png");
    m_backBtn->setParent(this);
    m_backBtn->move(this->width() - m_backBtn->width(),  this->height() - m_backBtn->height());

    QSound *backSound = new QSound(":/BackButtonSound.wav", this);
    QSound *clickLevelSound = new QSound(":/TapButtonSound.wav", this);

    connect(m_backBtn, &QPushButton::clicked, [=](){
        qDebug() << "clicked back button";
        backSound->play();
        emit hided();
    });

    //选择关卡按键
    for(int i = 0; i < 20; i++)
    {
        MyPushButton* btn = new MyPushButton(":/LevelIcon.png", ":/LevelIcon_select.png");
        btn->setParent(this);
        btn->move(25 + (i%4) * 70, 130 + (i/4) * 70);

        connect(btn, &QPushButton::clicked, [=](){

            clickLevelSound->play();
            PlayScene* playScene = new PlayScene(i +1);

            QTimer::singleShot(300, this, [=](){
                this->hide();
                playScene->show();
            });

            connect(playScene, &PlayScene::pressExitBtn, [=]()
            {

                QTimer::singleShot(300, this, [=]()
                {
                    playScene->hide();
                    playScene->close();
                    delete playScene;
                    this->show();
                });

            });
        });

        QLabel* label = new QLabel();
        label->setParent(this);
        label->setFixedSize(btn->width(), btn->height());
        label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        label->setText(QString::number(i + 1));
        label->move(25 + (i%4) * 70, 130 + (i/4) * 70);

        //设置鼠标穿透
        label->setAttribute(Qt::WA_TransparentForMouseEvents);
    }
}

void ChooseLevelScene::paintEvent(QPaintEvent *event)
{
    QPixmap pix;
    QPainter painter(this);
    pix.load(":/PlayLevelSceneBg.png");
    painter.drawPixmap(0, 0, this->width(), this->height(), pix);

    pix.load(":/Title.png");
    pix = pix.scaled(pix.width() * 0.7, pix.height() * 0.7);
    painter.drawPixmap((this->width() - pix.width()) * 0.5, 30, pix.width(), pix.height(), pix);
}



void ChooseLevelScene::resizeEvent(QResizeEvent* e)
{
    m_backBtn->move(this->width() - m_backBtn->width(),this->height() - m_backBtn->height());
}
